##############################
# Analysis of survey data
##############################

rm(list=ls())

library(foreign)
library(Hmisc)
library(ri)
library(stargazer)
library(readstata13)
library(Cairo)

##############################
# Prepare data
##############################

# load data
d17 = read.dta13("~/Dropbox/Cerrillos 2017/08_replication/Encuesta CEP 80 Jul-Ago 2017 v1.dta")
names(d17)

# explore data
names(d17)
head(d17)

######################
# Appendix E
#######################

# age
table(d17$edad_rec)
round(prop.table(table(d17$edad_rec)),2)

# gender
table(d17$ds_p1)
d17$female = 0
d17$female[d17$ds_p1=="Mujer"]=1
table(d17$female)
round(prop.table(table(d17$female)),2)

# educacion
table(d17$ds_p4)
d17$highschool = 0
d17$highschool[d17$ds_p4=="No Estudió"]=1
d17$highschool[d17$ds_p4=="Educación Básica Incompleta"]=1
d17$highschool[d17$ds_p4=="Educación Básica Completa"]=1
d17$highschool[d17$ds_p4=="Educación Media Incompleta"]=1
d17$highschool[d17$ds_p4=="Educación Media Completa"]=1
table(d17$highschool)
round(prop.table(table(d17$highschool)),2)

# vote next election
table(d17$mep_4)
d17$votenextelection = 0
d17$votenextelection[d17$mep_4=="Marco Enríquez-Ominami"]=1
d17$votenextelection[d17$mep_4=="Carolina Goic"]=1
d17$votenextelection[d17$mep_4=="Alejandro Guillier"]=1
d17$votenextelection[d17$mep_4=="José Antonio Kast"]=1
d17$votenextelection[d17$mep_4=="Franco Parisi"]=1
d17$votenextelection[d17$mep_4=="Sebastián Piñera"]=1
d17$votenextelection[d17$mep_4=="Beatriz Sánche"]=1
table(d17$votenextelection)
round(prop.table(table(d17$votenextelection)),2)

# vote past election
table(d17$ds_p13)
d17$votepastelection = 0 
d17$votepastelection[d17$ds_p13=="Sí"]=1
table(d17$votepastelection)
prop.table(table(d17$votepastelection))

# Candidates
table(d17$mep_4)
d17$candidates = 0
d17$candidates[d17$mep_4=="Sebastián Piñera"]=1
d17$candidates[d17$mep_4=="Alejandro Guillier"]=2
d17$candidates[d17$mep_4=="Beatriz Sánchez"]=3
table(d17$candidates)
round(prop.table(table(d17$candidates)),2)
